
package pimp.organizermanager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

public class Node implements TreeNode{

    String name;
    String value;
    public ArrayList<Node> childs = new ArrayList<Node>();
    Node parent;

    public Node(String name, Object value) {
        this.name = name;
        this.value = value.toString();
    }

    public Node(String name){
        this.name = name;
    }

    public void addChild(Node n){
        childs.add(n);
    }
    public Node getLastChild()
    {
        if(childs.size() > 0)
            return childs.get(childs.size()-1);
        else
            return null;
    }

    Node transformToNode(){
        return this;
    }

    @Override
    public String toString() {
        return name;
    }

    public String getName() {
        return name;
    }

    public String getValue() {
        return value;
    }

    //implementacja interfejsu TreeNode
    public Node getChildAt(int childIndex) {
        return childs.get(childIndex);
    }

    public int getChildCount() {
        return childs.size();
    }

    public TreeNode getParent() {
        return parent;
    }

    public int getIndex(TreeNode node) {

        for(int i=0; i< childs.size(); i++)
            if(childs.get(i).hashCode() == node.hashCode()) return i;

        return -1;

    }

    public boolean getAllowsChildren() {
       return true;
    }

    public boolean isLeaf() {
        return childs == null;
    }

    public Enumeration children() {
        return Collections.enumeration(childs);
    }

}
